<?php

/**
  @name ZFKiller_String
 */
class ZFKiller_String {

    public static function makeRandString($tamanho = 10, $maiusculas = true, $minusculas = true, $numeros = true, $simbolos = false) {
        $string = '';
        if ($maiusculas) {
            $string .= "ABCDEFGHIJKLMNOPQRSTUVYXWZ";
        }
        if ($minusculas) {
            $string .= "abcdefghijklmnopqrstuvyxwz";
        }
        if ($numeros) {
            $string .= "0123456789";
        }
        if ($simbolos) {
            $string .= "!@#$%¨&*()_+=";
        }

        $r = '';
        $min = 0;
        $max = strlen($string) - 1;
        for ($x = 0; $x < $tamanho; $x++) {
            $r .= $string[mt_rand($min, $max)];
        }

        return $r;
    }

    /**
     * Aplica uma máscara na string
     * String: "3438420000", Máscara: "(##)####-####", Resultado: "(34)3842-0000";
     * String: "12032010", Máscara: "##/##/##", Resultado: "12/03/2010";
     * String: "2236", Máscara: "##:##", Resultado: "22:36".
     * 
     * @author http://www.oficinadanet.com.br/artigo/php/mascara-de-formatacao-em-php
     * @param string $mascara
     * @param string $string
     * @return string 
     */
    public static function mask($mascara, $string) {
        $string = str_replace(" ", "", $string);
        for ($i = 0; $i < strlen($string); $i++) {
            $mascara[strpos($mascara, "#")] = $string[$i];
        }
        return $mascara;
    }

    public static function removeAcentos($string) {
        // http://www.vivaolinux.com.br/script/Funcao-Retira-Acentos-php
        $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç");
        $array2 = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c", "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C");
        return str_replace($array1, $array2, $string);
    }

    public static function removeNaoNumeros($string) {
        return preg_replace('/[^0-9]/','', $string);
    }

    public static function strToPermalink($string) {
        $string = trim($string);
        $string = ZFKiller_String::removeAcentos($string);
        $string = strtolower($string);
        $string = preg_replace('/[^a-z0-9_ \-]/', '', $string);
        $string = preg_replace('/[\-_ ]+/', '-', $string);
        return $string;
    }

    /**
     *
     * @link http://php.net/manual/pt_BR/function.fgetcsv.php
     * @param string $file
     * @param string $delimiter
     * @return array
     */
    public static function get2DArrayFromCsv($file, $delimiter, $hasHeader = true) {
        $data2DArray = array();
        if (($handle = fopen($file, "r")) !== FALSE) {
            $i = 0;
            $header = array();
            while (($lineArray = fgetcsv($handle, 4000, $delimiter)) !== FALSE) {
                if (($i == 0) and ($hasHeader)) {
                    $header = $lineArray;
                    $i++;
                    continue;
                }
                for ($j = 0; $j < count($lineArray); $j++) {
                    if ($hasHeader) {
                        $data2DArray[$i][$header[$j]] = $lineArray[$j];
                    }
                    else {
                        $data2DArray[$i][$j] = $lineArray[$j];
                    }
                }
                $i++;
            }
            fclose($handle);
        }
        return $data2DArray;
    }

    /**
     * Converte um array para string CSV
     * 
     * @param array $array
     * @param string $delimiter
     * @param string $enclosure
     * @param string $terminator
     * @return string
     * @link http://www.php.net/manual/en/function.str-getcsv.php
     */
    public static function getCsvFromArray(array $array, $delimiter = ',', $enclosure = '"', $terminator = "\n", $addHeader = true) {
        # First convert associative array to numeric indexed array 
        $workArray = array_values($array);

        $returnString = '';                 # Initialize return string 
        $arraySize = count($workArray);     # Get size of array 

        for ($i=0; $i<$arraySize; $i++) { 
            # Nested array, process nest item 
            if (is_array($workArray[$i])) {
                if (($addHeader) and ($i == 0)) {
                    $returnString .= self::getCsvFromArray(array_keys($workArray[$i]), $delimiter, $enclosure, $terminator);
                }
                $returnString .= self::getCsvFromArray($workArray[$i], $delimiter, $enclosure, $terminator);
            } else {
                switch (gettype($workArray[$i])) {
                    # Manually set some strings 
                    case "NULL":     $_spFormat = ''; break; 
                    case "boolean":  $_spFormat = ($workArray[$i] == true) ? 'true': 'false'; break; 
                    # Make sure sprintf has a good datatype to work with 
                    case "integer":  $_spFormat = '%i'; break; 
                    case "double":   $_spFormat = '%0.2f'; break; 
                    case "string":   $_spFormat = '%s'; break; 
                    # Unknown or invalid items for a csv - note: the datatype of array is already handled above, assuming the data is nested 
                    case "object":
                    case "resource":
                    default:         $_spFormat = ''; break; 
                }
                                $returnString .= sprintf('%2$s'.$_spFormat.'%2$s', $workArray[$i], $enclosure); 
                                $returnString .= ($i < ($arraySize-1)) ? $delimiter : $terminator; 
            }
        }
        # Done the workload, return the output information 
        return $returnString;
    }

}